﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ModelPredictPro.General {
    public class Sample {
        public double? Label { get; set; }
        public Variable[] Variables { get; private set; }
        public int Dimension { get; private set; }

        public Sample(double ? label, Variable[] variables) {
            Label = label;
            Variables = variables;
            this.Dimension = variables.Length;
        }


        public static explicit operator Sample(string input) {
            if (input == null || (input=input.Trim()) == "") {
                throw new FormatException("不能将空字符串转换为Sample类型的实例！");
            }
            
            string[] temp = input.Split(default(char[]), 2, StringSplitOptions.RemoveEmptyEntries);
            double? label = null;
            Variable[] vs = null;
            try {
                if (temp[0].Contains(":")) {
                    vs = Variable.ParseFromIndexedValueString(input);
                }
                else {
                    label = double.Parse(temp[0]);
                    vs = Variable.ParseFromIndexedValueString(temp[1]);
                }
            }
            catch {
                throw new ArgumentException(string.Format("传入强制转换为Sample的input : {0} 格式不正确！", input));
            }
            Sample rst = new Sample(label, vs);
            return rst;
        }
    }
}
